﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsifWeb4.Models;

namespace GsifWeb4.Controllers
{
    public class ForumController : Controller
    {
        private gsifdbEntities db = new gsifdbEntities();

        //
        // GET: /Forum/

        public ActionResult Index(int forumType)
        {
            string headerText = "";
            if (ForumType.Main.IsDbValue(forumType.ToString()))
                headerText = "Välkommen till GSIFs klotterplank!";
            else if (ForumType.MenA.IsDbValue(forumType.ToString()))
                headerText = "Välkommen till GSIFs forum för herrarnas A-lag!";
            else if (ForumType.MenC.IsDbValue(forumType.ToString()))
                headerText = "Välkommen till GSIFs forum för herrarnas C-lag!";
            else if (ForumType.WomenA.IsDbValue(forumType.ToString()))
                headerText = "Välkommen till GSIFs forum för damernas A-lag!";
            else if (ForumType.WomenB.IsDbValue(forumType.ToString()))
                headerText = "Välkommen till GSIFs forum för damernas B-lag!";

            
            ViewBag.HeaderText = headerText;
            ViewBag.ForumType = forumType;

            var messages = db.forum_gsif.Where(f => f.ForumType == forumType).Take(50).ToList().OrderByDescending(f => f.Datum);
            return View(messages);
        }

        //
        // GET: /Forum/Details/5

        public ActionResult Details(int id = 0)
        {
            forum_gsif forum_gsif = db.forum_gsif.Single(f => f.Id == id);
            if (forum_gsif == null)
            {
                return HttpNotFound();
            }
            return View(forum_gsif);
        }

        //
        // GET: /Forum/Create

        public ActionResult Create(int forumType)
        {
            ViewBag.ForumType = forumType;
            return View();
        }

        //
        // POST: /Forum/Create

        [HttpPost]
        public ActionResult Create(forum_gsif forum_gsif)
        {
            if (ModelState.IsValid)
            {
                forum_gsif.Id = Sequence.GetNextFromSequence(db, Sequence.ForumId);
                forum_gsif.Datum = DateTime.Now;
                forum_gsif.Ip = Request.UserHostAddress;
                db.forum_gsif.AddObject(forum_gsif);
                db.SaveChanges();
                return RedirectToAction("Index", new {ForumType = forum_gsif.ForumType });
            }

            return View(forum_gsif);
        }

        //
        // GET: /Forum/Edit/5

        public ActionResult Edit(int id = 0)
        {
            forum_gsif forum_gsif = db.forum_gsif.Single(f => f.Id == id);
            if (forum_gsif == null)
            {
                return HttpNotFound();
            }
            return View(forum_gsif);
        }

        //
        // POST: /Forum/Edit/5

        [HttpPost]
        public ActionResult Edit(forum_gsif forum_gsif)
        {
            if (ModelState.IsValid)
            {
                db.forum_gsif.Attach(forum_gsif);
                db.ObjectStateManager.ChangeObjectState(forum_gsif, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(forum_gsif);
        }

        //
        // GET: /Forum/Delete/5

        public ActionResult Delete(int id = 0)
        {
            forum_gsif forum_gsif = db.forum_gsif.Single(f => f.Id == id);
            if (forum_gsif == null)
            {
                return HttpNotFound();
            }
            return View(forum_gsif);
        }

        //
        // POST: /Forum/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            forum_gsif forum_gsif = db.forum_gsif.Single(f => f.Id == id);
            db.forum_gsif.DeleteObject(forum_gsif);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}